package mvc;

import java.util.List;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;




@Controller
public class WebController {
	ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
	MessageJDBCTemplate jdbc = (MessageJDBCTemplate) context.getBean("messageJDBCTemplate");
	@RequestMapping(value="/",method = RequestMethod.GET)
	public String index() {
		return "index";
	}
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(ModelMap m) {
		List<Message> list = jdbc.listMessages();
        m.addAttribute("list",list);
		return "list";
	}
	@RequestMapping(value="/delete",method = RequestMethod.GET)
	public String delete(String id) {
		if(id.equals("")) return "error";
		String[] arr = id.split(",");
		for (String i : arr) {
			Integer result = jdbc.delete(Integer.parseInt(i));
			System.out.println(result);
			if(result.equals(0)) return "error";
		}
	    return "redirect:list";
	}
}
